package com.yym.jni;

import com.start.printer.HelloJNI;
import com.start.printer.NativeLoader;
import com.start.printer.utils.BitmapUtils;
import com.start.printer.utils.FileUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.awt.*;
import java.awt.image.BufferedImage;

@RestController
@SpringBootApplication
public class JniApplication {

    public static void main(String[] args) {
        SpringApplication.run(JniApplication.class, args);
    }

    /**
     * 启动加载一次native库
     */
    @Bean
    public void loadLib() {
        //根据操作系统判断，如果是linux系统则加载c++方法库
        String systemType = System.getProperty("os.name");
        String ext = (systemType.toLowerCase().indexOf("win") != -1) ? ".dll" : ".so";
        if (ext.equals(".so")) {
            try {
                NativeLoader.loader("native");
            } catch (Exception e) {
                System.out.println("加载so库失败");
            }
        }
        System.out.println("loaded");
    }

    @RequestMapping("/")
    public String hello() {
        HelloJNI.setNum(20);
        return "load jni get " + HelloJNI.get();
    }

    @RequestMapping("/bmp/{fileName}")
    public String bmpParse(@PathVariable String fileName) {
        // /root/native/24.bmp
        BufferedImage bufferedImage = BitmapUtils.readImage("/root/native/" + fileName);
        BufferedImage pageBitmap = BitmapUtils.createPageBitmap(bufferedImage, new Point(40, 40), new Point(200, 200));
        int width = pageBitmap.getWidth();
        int height = pageBitmap.getHeight();
        //原图像素
        byte[] piexs = BitmapUtils.readImagePiex(pageBitmap);
        //处理过的像素
        byte[] otsu = HelloJNI.otsu(width, height, piexs, -1);
        FileUtil.getFile(otsu,"/root/native/","24.prn");
        return "图片处理完成";
    }
}
